package com.yu.Demo04;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class SyncLockDemo {
    /**
     * sync来证明可重入锁
     * @param args
     */
//    public static void main(String[] args) {
//        Object o = new Object();
//        new Thread(()->{
//            synchronized (o){
//                System.out.println(Thread.currentThread().getName()+"外层");
//
//                synchronized (o){
//                    System.out.println(Thread.currentThread().getName()+"内层");
//                }
//            }
//        },"T1").start();
//    } //T1外层
//      //T1内层  证明了获取了外层的，就会直接获取内层锁
//
//
//
//    //执行会内存异常，证明了sync是可有循环递归调用的
//    public synchronized void add(){
//        add();
//    }
    public static void main(String[] args) {

        Lock lock = new ReentrantLock();

        try {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + ":外层");
            try {
                lock.lock();
                System.out.println(Thread.currentThread().getName() + ":内层");
            }finally {
                lock.unlock();
            }
        }catch (Exception e){

        }finally {
            lock.lock();
        }

    }






}

